﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.ComponentModel;

namespace MLY.Bll
{
    [DataObject]
    public class clsOrganizationRelation
    {

        #region Constructors
        // ==================================
        public clsOrganizationRelation()
        {
            _db = new Dal.lqKbmDataContext();
            _DataRow = new Dal.TableOrganizationRelation();
            IsExist = false;
        }

        public clsOrganizationRelation(int ClientId, int ContractorId)
        {
            //TODO: Check privilege
            _db = new Dal.lqKbmDataContext();
            _DataRow = (from o in _db.TableOrganizationRelations
                        where ((o.CustomerId == ClientId) && (o.ContractorId == ContractorId))
                        select o).FirstOrDefault();
            IsExist = _DataRow != null ? true : false;

           
        }
        // ==================================
        #endregion

        #region Static Members
        // ==================================
        // ==================================
        #endregion

        #region Public Methods
        // ==================================
        public static List<Bll.clsMessageRelationsContacts> GetOrgContractorAndClients(int OrgId)
        {
            List<Bll.clsMessageRelationsContacts> retval = new List<clsMessageRelationsContacts>();
            MLY.Dal.lqKbmDataContext dt = new MLY.Dal.lqKbmDataContext();            

            var q =
                    (from orgRelation in dt.TableOrganizationRelations
                     join org in dt.TableOrganizations
                     on orgRelation.ContractorId equals org.OrganizationId
                     where orgRelation.CustomerId == OrgId
                     select new Bll.clsMessageRelationsContacts(orgRelation.ContractorId.Value, org.OrganizationName)).ToList();
            var t =
                    (from orgRelation in dt.TableOrganizationRelations
                     join org in dt.TableOrganizations
                     on orgRelation.CustomerId equals org.OrganizationId
                     where orgRelation.ContractorId == OrgId
                     select new Bll.clsMessageRelationsContacts(orgRelation.CustomerId.Value, org.OrganizationName)).ToList();
            List<Bll.clsMessageRelationsContacts> t2 = q.Union(t).ToList();
            IEnumerable<System.Linq.IGrouping<int, clsMessageRelationsContacts>> myGroup = t2.GroupBy(w => w.GetOrgId);
            foreach (System.Linq.IGrouping<int, clsMessageRelationsContacts> i in myGroup)
                retval.Add(i.ElementAt(0));
            return retval;
        }

        //public static List<Bll.clsMessageRelationsContacts> GetOrgSubContractors(int OrgId)
        //{
        //    using (MLY.Dal.lqKbmDataContext dt = new MLY.Dal.lqKbmDataContext())
        //    {
        //        List<Bll.clsMessageRelationsContacts> OrgSubContractors = (from orgRelation in dt.TableOrganizationRelations
        //                                                                   join organization in dt.TableOrganizations
        //                                                                   on orgRelation.ContractorId equals organization.OrganizationId
        //                                                                   where orgRelation.CustomerId == OrgId
        //                                                                   select new Bll.clsMessageRelationsContacts(orgRelation.ContractorId.Value, organization.OrganizationName)).ToList();
        //        return OrgSubContractors;
        //    }

        //}

        //checks if a relation exists
        public static bool CheckOrgRelation(int Client,int ContractorId)
        {
            using (MLY.Dal.lqKbmDataContext dt = new MLY.Dal.lqKbmDataContext())
            {
                var OrgSubContractors = (from orgRelation in dt.TableOrganizationRelations
                             join org in dt.TableOrganizations
                             on orgRelation.ContractorId equals org.OrganizationId
                                         where ((orgRelation.CustomerId == Client) && (orgRelation.ContractorId == ContractorId))                             
                             select orgRelation).FirstOrDefault();

                if (OrgSubContractors != null) return true;
                else return false;
            }
            
        }

        public static bool DeleteRelation(int OrganizationId, int Contractor)
        {
            using (Dal.lqKbmDataContext db = new MLY.Dal.lqKbmDataContext())
            {
                if (getWorkOrdersCount(OrganizationId, Contractor) == 0)
                {
                    Dal.TableOrganizationRelation row =
                    (from orgRl in db.TableOrganizationRelations
                     where orgRl.ContractorId == Contractor && orgRl.CustomerId == OrganizationId
                     select orgRl).FirstOrDefault();

                    db.TableOrganizationRelations.DeleteOnSubmit(row);
                    db.SubmitChanges();
                    return true;
                }
                else return false;
            }
        }

        public static int getWorkOrdersCount(int OrganizationId, int Contractor)
        {
         
            using (Dal.lqKbmDataContext db = new MLY.Dal.lqKbmDataContext())
            {
                List<Dal.TableWorkOrder> WOLst = (from p in db.TableWorkOrders
                                                          where p.CustomerId == OrganizationId && p.ContractorId == Contractor && p.ActualDelivaryDate == null 
                                                          select p).ToList();                                
                 return WOLst.Count;             
            }

        }


        public void Save()
        {
            if (IsExist)
            {
                _db.SubmitChanges();
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdateBy = clsUser.CurrentUserId;
            }
            else
            {
                _DataRow.DateCreated = DateTime.Now;
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdateBy = clsUser.CurrentUserId;

                _db.TableOrganizationRelations.InsertOnSubmit(_DataRow);
                _db.SubmitChanges();
            }
        }
        // ==================================
        #endregion

        #region Public Properties
        // ==================================
        public Dal.TableOrganizationRelation Data
        {
            get { return _DataRow; }
        }
        // ==================================
        #endregion

        #region Fields
        // ==================================
        public bool IsExist = false;
        Dal.lqKbmDataContext _db = null;
        Dal.TableOrganizationRelation _DataRow = null;
        // ==================================
        #endregion
    }
}